import { objDeepCopy,getDateString } from "@/assets/js/common.js";
let Controller = {
	created() {
		this.initdata();
	},
	mounted() {
		this.init();
	},
	activated() {
		this.init();
		this.initdata();
	},
	methods: {
		init() {
			this.Model.Outofstock = this.$route.query.flag;
			this.Model.OutofstockBtn = this.$route.query.status;
		},
		initdata() {
			this.Model.orderNumber = this.$route.query.id;		//订单id
			this.requestDetail(this,this.Model.orderNumber);
		},
		//详情接口
		requestDetail(that,id) {
			this.$ajax({
				method: 'post',
				//url:'/api/supplier/whWarehouseDetail/outAndInDetails',
				url:this.apiTest+'/supplier/whWarehouseDetail/outAndInDetails',
				data:{
					condition:{
						number:id
					}
				}
			}).then((res)=>{
				var data = res.data;
				if(data.status == 200){
					var result = data.result;
					console.log(result)
					this.Model.whWarehouseId = result.whWarehouseId;
					//应领商品
					that.Model.tableData = objDeepCopy(result.shouldSaleGoodsInfoVos);
					//实领商品
					that.Model.tableDataOutNews = objDeepCopy(result.actualSaleGoodsInfoVos);
					//操作日志
					that.Model.tableData4 = objDeepCopy(result.whWarehouseLogList);
					//表头信息
					that.Model.tabFormInline = [{
							label: '领料单号',
							value: result.number,
						},
						{
							label: '单据状态',
							value: result.status,
						},
					];
					//出库单基础信息
					that.Model.tableFormInline = [{
							label: '单据类型',
							value: '',
						},
						{
							label: '采购单号',
							value: '',
						},
						{
							label: '供应商',
							value: ''
						},
						{
							label: '加工类型',
							value: result.type
						},
						{
							label: '品类',
							value: ''
						},
						{
							label: '款式名称',
							value:''
						},
						{
							label: '定制编码',
							value: ''
						},
						{
							label: '操作人',
							value: ''
						},
						{
							label: '导出次数',
							value: ''
						},
						{
							label: '打印次数',
							value: ''
						},
						{
							label: '备注',
							value: ''
						}
					];
				}else{
					that.tips(that,'warning',data.msg)
				}
			
			});
		},
		setStepData(list, status) {
			
		},
		//请求扫码入库 
		skuEntryAjax(that,code){
			this.$ajax({
					method: 'post',
					url:this.apiTest+'/supplier/whWarehouseDetail/getByKeyWord',
					//url:'/api/supplier/whWarehouseDetail/getByKeyWord',
					data:{
						keyWord:code,
						whWarehouseId:this.Model.whWarehouseId
					}
			    }).then((res)=>{
					var data = res.data;
					if(data.status == 200){
						var obj = {};
						var result = data.result;
						if(result){
							console.log(result)
							obj = objDeepCopy(result);
							that.Model.tableDataOut.push(obj)
						}
					}else{
			
					}
			
				});
		},
		
		//扫码入库
		checkSku(){
			var code = this.Model.writeSku;
			var tableDataOut = this.Model.tableDataOut;
			var codeIndexOf = this.valueInData(code,tableDataOut);
			if(codeIndexOf == -1){
				this.skuEntryAjax(this,code);
			}else{
				this.tips(this,'warning','条码已存在')
			}
		},
		//遍历 判断是否存在数组中
		valueInData(val,data){
			var indexof = -1;
			data.forEach(function(value,index,array){
				Object.keys(value).forEach(function(key){
					// console.log(key,value[key]);
					if(key == 'batchNo' && value[key] == val){
						indexof = 0;
						return;
					}
				});
			})
			return indexof;
		},
		//删除 表格数据
		deleteTableData(){
			var that = this;
			var TableSelectData = this.Model.TableSelectData;
			var tableDataOut = this.Model.tableDataOut;
			if(TableSelectData.length == 0){
				this.tips(this,'warning','请先勾选需要删除的数据');
				return;
			}
			this.$confirm('此操作将删除相应数据, 是否继续?', '提示', {
			  confirmButtonText: '确定',
			  cancelButtonText: '取消',
			}).then(() => {
				TableSelectData.forEach(function(value,index,array){
					tableDataOut.forEach(function(val,idx,arr){
						if(value == val){
							that.Model.tableDataOut.splice(idx,1);
						}
					})
				})
				this.$message({
					type: 'success',
					message: '删除成功!'
				});
			}).catch(() => {
			           
			});
		},
		//点击导入物料按钮
		importMaterial(){
			this.Model.importFileDialog = true;
		},
		//选择文件
		fileChange(){
			var fileObj = document.getElementById("fileup").files[0];
			var form = new FormData();
			var that = this;
			var obj = document.getElementById("fileup");
			var len = obj.files.length;
			for (var i = 0; i < len; i++) {
				var temp = obj.files[i].name;
				that.Model.fileupName = temp;
			}
			form.append("file", fileObj,that.Model.fileupName);
			form.append('number', that.Model.orderNumber);
			this.Model.upFileForm = form;
		},
		//点击上传
		uploadExcel(){
			var tableDataOut = this.Model.tableDataOut;
			var that = this;
			this.$ajax({
				method: 'post',
				//url:'/api/supplier/whWarehouseDetail/importMaterialsWarehouseExcel',
				url:this.apiTest+'/supplier/whWarehouseDetail/importMaterialsWarehouseExcel',
				data:this.Model.upFileForm
			}).then((res)=>{
				console.log(res);
				var obj = {};
				if(res.data.status == 200){
					var result = res.data.result;
					if(result){
						for (let i = 0; i < result.length; i++) {
							var indexof = -1;
							obj = objDeepCopy(result[i]);
							if(tableDataOut.length == 0){
								that.Model.tableDataOut.push(obj);
							}else {
								tableDataOut.forEach(function(value,index,array){
									if(value.batchNo == obj.batchNo){
										indexof = 0;
									}
								})
								if(indexof == -1){
									that.Model.tableDataOut.push(obj);
								}
							}
							
						}
					}
					setTimeout(function(){
						that.Model.importFileDialog = false;
						that.tips(that,'success','导入成功！')
					},1000);
				}else{
					that.tips(that,'warning','导入失败！')
				}
			});
		},
		//下载模板
		downloadExcel(){
			this.$ajax({
				method: 'get',
				//url:'/api/supplier/whWarehouseDetail/getGoodsExcelInfo',
				url:this.apiTest+'/supplier/whWarehouseDetail/getGoodsExcelInfo',
			}).then((res)=>{
				console.log(res);
				if(res.data.status == 200){
					var result = res.data.result;
					if(result.fileUrl){
						window.open(result.fileUrl);
					}
					
				}else{
					
				}
			});
		},
		//出库
		Changeoutofstock() {
			this.Model.Outofstock = '0';
			this.Model.OutofstockBtn = '1';
		},
		//确认出库
		ToOutofstock() {
			var arr = [];
			var tableDataOut = this.Model.tableDataOut;
			for (let i = 0; i < tableDataOut.length; i++) {
				arr.push(tableDataOut[i].id);
			}
			if(tableDataOut.length == 0){
				this.tips(this,'warning','请选择数据');
				return;
			}
			//提交的数据 formInline
			var formInline = this.Model.formInline;
			formInline.ids = arr;
			formInline.whWarehouseId = this.Model.whWarehouseId;
			console.log(formInline)
			this.confirmNewAjax(this,formInline);
		},
		//确认出库 请求
		confirmNewAjax(that,params){
			this.$ajax({
				method: 'post',
				url:this.apiTest+'/supplier/whWarehouseDetail/outWarehouse',
				//url:'/api/supplier/whWarehouseDetail/outWarehouse',
				data:params
			}).then((res)=>{
				var data = res.data;
				if(data.status == 200){
					var result = data.result;
					if(result == 'OK'){
						that.tips(that,'success','操作成功');
						this.requestDetail(this,this.Model.orderNumber);
						this.$router.push({
							path:'receiveMaterialDetail',
							query: {
								id:this.Model.orderId,
								flag:"1",
								status:"1"
							}
						});
					}else{
						that.tips(that,'warning',result)
					}
				}else{
					that.tips(that,'warning',data.msg)
				}
		
			});
		},
		//取消
		goBack() {
			this.$router.back(-1);
		},
		warehousing(row) {
			this.Model.warehousingDialogStatus = row.status;
			this.Model.warehousingDialogFlag = true;
		},
		handleSelectionChange(val) {
			this.Model.TableSelectData = val;
		},
		handleCurrentChange(val) {
			this.Model.pager.params.pageIndex = val;
		},
		handleSizeChange(val) {
			this.Model.pager.params.pageSize = val;
		},
		cardClick(color) {
			this.Model.cardSelected = color;
		},
		showChild(val) {
			this.Model.searchFromData = val;
		},
		toSearch(val) {
			console.log(val)
		},
		toClear(val) {
			this.Model.searchFromData = this.Model.defaultSearchFromData;
		},
		toExport(val) {
			console.log(val);
		},
		handleChange() {
			
		},	
		onTabChange(val) {
			switch (val) {
				case 1:
					break;
				case 2:
					break;
				case 3:
					break;
			}
		},
		//展开收缩
		toggleTableDetail(val) {
			const obj = this.Model[val];
			if (obj.flag === true) {
				this.Model[val].src = require('@/assets/img/icon_collapse.png');
				this.Model[val].flag = false;
			} else {
				this.Model[val].src = require('@/assets/img/icon_unfold.png');
				this.Model[val].flag = true;
			}
		},
		tips(that,type,message) {
			that.$message({
				message: message,
				type: type
			});
		},
	}
}
export default Controller;
